package com.aliyun.messagemgt.repository;

import com.aliyun.messagemgt.converter.BaseMessageTemplateConverter;
import com.aliyun.messagemgt.domain.repository.IBaseMessageTemplateService;
import com.aliyun.messagemgt.dto.BaseMessageTemplateDTO;
import com.aliyun.messagemgt.dto.query.MessageTemplateQuery;
import com.aliyun.messagemgt.entities.BaseMessageTemplate;
import com.aliyun.messagemgt.repository.mappers.BaseMessageTemplateMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 基础表-消息模板参数表 服务实现类
 * </p>
 *
 * @author wugongzi
 * @since 2021-09-15
 */
@Service
public class BaseMessageTemplateServiceImpl implements IBaseMessageTemplateService {

    @Autowired
    private BaseMessageTemplateMapper baseMessageTemplateMapper;


    @Override
    public BaseMessageTemplateDTO getByTemplateCode(String templateCode) {
        QueryWrapper<BaseMessageTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("template_code", templateCode);
        queryWrapper.eq("is_enabled", "Y");
        queryWrapper.eq("is_deleted", "N");
        BaseMessageTemplate baseMessageTemplate = baseMessageTemplateMapper.selectOne(queryWrapper);
        BaseMessageTemplateDTO baseMessageTemplateDTO = BaseMessageTemplateConverter.INSTANCE.baseMessageTemplate2baseMessageTemplateDTO(baseMessageTemplate);
        return baseMessageTemplateDTO;
    }

    @Override
    public BaseMessageTemplateDTO getByTemplateId(String templateId) {
        QueryWrapper<BaseMessageTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", templateId);
        BaseMessageTemplate baseMessageTemplate = baseMessageTemplateMapper.selectOne(queryWrapper);
        BaseMessageTemplateDTO baseMessageTemplateDTO = BaseMessageTemplateConverter.INSTANCE.baseMessageTemplate2baseMessageTemplateDTO(baseMessageTemplate);
        return baseMessageTemplateDTO;
    }

    @Override
    public BaseMessageTemplateDTO getTemplate(MessageTemplateQuery qry) {
        BaseMessageTemplate baseMessageTemplateQry = BaseMessageTemplateConverter.INSTANCE.messageTemplateQuery2baseMessageTemplate(qry);
        QueryWrapper<BaseMessageTemplate> queryWrapper = new QueryWrapper<>(baseMessageTemplateQry);
        BaseMessageTemplate baseMessageTemplate = baseMessageTemplateMapper.selectOne(queryWrapper);
        BaseMessageTemplateDTO baseMessageTemplateDTO = BaseMessageTemplateConverter.INSTANCE.baseMessageTemplate2baseMessageTemplateDTO(baseMessageTemplate);
        return baseMessageTemplateDTO;
    }
}
